<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="utf-8"> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <title>查看公司详情</title> 


    <link type="text/css" href="/TopJUI/topjui/css/topjui.core.min.css" rel="stylesheet">
    <link type="text/css" href="/TopJUI/topjui/themes/default/topjui.green.css" rel="stylesheet" id="dynamicTheme"/>
    <!-- FontAwesome字体图标 -->
    <link type="text/css" href="/TopJUI/topjui/plugins/font-awesome/css/font-awesome.min.css" rel="stylesheet"/>
        <!-- 图片上传样式 -->
    <link type="text/css" href="/TopJUI/upload/css/upload.css" rel="stylesheet"/>
    <!-- jQuery相关引用 -->
    <script type="text/javascript" src="/TopJUI/topjui/plugins/jquery/jquery.min.js"></script>
    <script type="text/javascript" src="/TopJUI/topjui/plugins/jquery/jquery.cookie.js"></script>
    <!-- TopJUI框架配置 -->
    <script type="text/javascript" src="/TopJUI/static/public/js/topjui.config.js"></script>
    <!-- TopJUI框架核心-->
    <script type="text/javascript" src="/TopJUI/topjui/js/topjui.core.min.js"></script>
    <!-- TopJUI中文支持 -->
    <script type="text/javascript" src="/TopJUI/topjui/js/locale/topjui.lang.zh_CN.js"></script>

    <script type="text/javascript" src="/pages/js/moment.min.js"></script>
    <script type="text/javascript" src="/pages/js/layer/layer.js"></script>
    <script type="text/javascript" src="/pages/js/util.js"></script>
    <script type="text/javascript" src="/pages/js/shuoheUtil.js"></script>
    
    <!-- 图片上传js -->
    <script type="text/javascript" src="/TopJUI/upload/js/upload.js"></script>
    
    
<style type="text/css">
    html, body{ margin:0; height:100%;width: 100%;overflow: auto;}
</style>



<body onkeydown='onEnterDown();'>
    <table class="editTable" style="text-align: center;" >
        <tr>
            <td class="label" colspan="4" style="text-align: left;font-size: 20px;">
                <div class="divider">
                    <span style="margin-left: 50px">设备类型信息</span>
                </div>
            </td>
        </tr>               
        <tr>
            <td class="label">设备编号</td>
            <td >
                <input id='equipment_id' class="easyui-textbox" data-options="required:false,width:200,editable:true" disabled>
            </td>
            <td class="label">设备名称</td>
            <td >                
                <input id="equpiment_name" class="easyui-combogrid"
                    data-options="   
                        width:200,                        
                        panelWidth:600,   
                        panelHeight:300,   
                        mode: 'remote',
                        idField:'id',      
                        textField:'equipment_type_name',          
                        columns:[[   
                            {field:'equipment_type_name',title:'设备名称'},   
                            {field:'equipment_model',title:'设备型号'},   
                            {field:'manufacturer',title:'厂家'},
                            {field:'care_type_text',title:'保养类别'},
                            {field:'eqp_type_text',title:'设备分类'}
                        ]],
                        prompt:'输入可模糊查询设备名称'
                " disabled></input> 
            </td>                
            </td>
        </tr>
        <tr>
            <td class="label">设备型号</td>
            <td >
                <input id='equipment_model' class="easyui-textbox" data-options="required:false,width:200,editable:true" disabled>
            </td>
            <td class="label">保养类别</td>
            <td >
                <input id='care_type' class="easyui-textbox" data-options="required:false,width:200,editable:false" disabled>
            </td>            
        </tr>
        <tr>
            <td class="label">设备分类</td>
            <td >
                <input id='eqp_type' class="easyui-textbox" data-options="required:false,width:200,editable:false" disabled>
            </td>        
            <td class="label">制造厂家</td>
            <td >
                <input id='manufacturer' class="easyui-textbox" data-options="required:false,width:200,editable:false" disabled>
            </td>                           
        </tr>
        <tr>
            <td class="label">区域</td>
            <td >
                <input id='place_area' class="easyui-combobox" data-options="required:false,width:200,editable:true" disabled>
            </td>        
            <td class="label">放置位置</td>
            <td >
                <input id='place_position' class="easyui-textbox" data-options="required:false,width:200,editable:true" disabled>
            </td>                           
        </tr>
        <tr>
            <td class="label">使用日期</td>
            <td >
                <input id='start_using_date' class="easyui-datebox" data-options="required:false,width:200,editable:true" disabled>
            </td>        
            <td class="label">控制系统型号</td>
            <td >
                <input id='control_system_model' class="easyui-textbox" data-options="required:false,width:200,editable:true" disabled>
            </td>                           
        </tr>
        <tr>
            <td class="label">当前状态</td>   
            <td >
                <input id='status' class="easyui-combobox" data-options="required:false,width:200,editable:true" disabled>
            </td> 
            <td class="label">设备资产编号</td>   
            <td >
                <input id='equipment_property_number' class="easyui-textbox" data-options="required:false,width:200,editable:true" disabled>
            </td>        
        </tr> 
        <tr>
            <td class="label">责任人</td>   
            <td >
                <input id='person_liable_id' class="easyui-combobox" data-options="required:false,width:200,editable:true" disabled>
            </td>         
        </tr> 
    </table>
    <div class="upload-box">
	    <p class="upload-tip">设备图片</p>
	    <div class="image-box clear">
<!-- 	        <section class="upload-section"> -->
<!-- 	            <div class="upload-btn"></div> -->
<!-- 	            <input type="file" name="file" id="upload-input" value="上传" accept="image/jpg,image/jpeg,image/png,image/bmp"/> -->
<!-- 	        </section> -->
	    </div>
    </div>
</body> 
</html>
<script type="text/javascript">

    var equipment = new Object();
    var equipment_id = shuoheUtil.getUrlHeader().equipment_id;
    $(function() {
        $('#equpiment_name').combogrid({
            url: '/develop/url/getUrl.do',
            queryParams: {
                'name': 'queryEqupimentLikeName'
            },
            onSelect: function(ret) {
                // console.info(JSON.stringify(ret));
                var g = $('#equpiment_name').combogrid('grid'); // 获取表格控件对象
                var r = g.datagrid('getSelected'); //获取表格当前选中行
                console.info(JSON.stringify(r));
                equipmentType = r;
                $('#equipment_type_name').textbox('setValue', r.equipment_type_name);
                $('#equipment_model').textbox('setValue', r.equipment_model);
                $('#care_type').textbox('setValue', r.care_type_text);
                $('#eqp_type').textbox('setValue', r.eqp_type_text);
                $('#manufacturer').textbox('setValue', r.manufacturer);
                loadMaintainProject(r.id);

            }
        });

        $('#place_area').combobox({
            url: '/develop/url/getUrl.do',
            valueField: 'id',
            textField: 'text',
            panelHeight: 200,
            method: 'get',
            multiple: false,
            queryParams: {
                'name': 'selectEquipmentPlaceArea'
            },
            onLoadSuccess: function() {},
            onSelect: function(ret) {
            }
        });

        $('#status').combobox({
            url: '/develop/url/getUrl.do',
            valueField: 'id',
            textField: 'text',
            panelHeight: 200,
            method: 'get',
            multiple: false,
            queryParams: {
                'name': 'selectEquipmentStatusList'
            },
            onLoadSuccess: function() {},
            onSelect: function(ret) {}
        });
        
      //获取责任人
        $('#person_liable_id').combobox({
            url: '/develop/url/getUrl.do',
            valueField: 'id',
            textField: 'text',
            panelHeight: 200,
            method: 'get',
            multiple: false,
            queryParams: {
                'name': 'getPersonLiable'
            },
            onLoadSuccess: function() {}
        });

        $('#dg').datagrid({
            onAfterEdit: function(rowIndex, rowData, changes) {
                console.info('onAfterEdit:rowIndex = ' + rowIndex);
                console.info('onAfterEdit:rowData = ' + JSON.stringify(rowData));
                console.info('onAfterEdit:changes = ' + JSON.stringify(changes));
                console.info('000000:changes = ' + rowData.maintain_period_month);
                console.info('111111:changes = ' + shuoheUtil.checkNull(rowData.maintain_last_date));
                console.info('222222:changes = ' + shuoheUtil.checkNull(rowData.maintain_period_month));

                if (shuoheUtil.checkNull(rowData.maintain_last_date)==false 
                    && shuoheUtil.checkNull(rowData.maintain_period_month)==false) {

                    console.info('onAfterEdit:rowIndex 11');

                    var maintain_next_date = shuoheUtil.getXMonthLaterDateYMD(rowData.maintain_last_date, rowData.maintain_period_month);

                    $('#dg').datagrid('updateRow', {
                        index: rowIndex,
                        row: {
                            maintain_next_date: maintain_next_date
                        }
                    })

                }
                else
                {
                    console.info('onAfterEdit:rowIndex 22');
                    parent.$('#dg').datagrid('updateRow', {
                        index: rowIndex,
                        row: {
                            maintain_next_date: ''
                        }
                    })



                }               
            }
        });
        init();
        loadAllImage();//加载设备的所有图片
    });

    function init() {
        $.ajax({
            url:'/develop/url/getUrl.do',
            type: "POST",
            dataType: 'json',
            data: {
                'name': 'findEquipmentById',
                'id':shuoheUtil.getUrlHeader().id
            },
            error: function() //失败
            {
            },
            success: function(data) //成功
            {

                
                $('#equipment_id').textbox('setValue',data.equipment_id);
                $('#equpiment_name').combogrid('setText', data.equipment_type_name);
                $('#care_type').textbox('setValue',data.maintain_class_text);
                $('#eqp_type').textbox('setValue',data.equipment_classify_text);
                $('#equipment_model').textbox('setValue',data.equipment_model);
                $('#manufacturer').textbox('setValue',data.manufacturer);
                
                $('#place_position').textbox('setValue',data.place_position);
                $('#start_using_date').datebox('setValue',data.start_using_date);
                $('#control_system_model').textbox('setValue',data.control_system_model);
                $('#equipment_property_number').textbox('setValue',data.equipment_property_number);
                $('#person_liable_id').combobox('setValue',data.person_liable_id);

                if(data.place_area != 0)
                    $('#place_area').combobox('setValue',data.place_area);
                if(data.status != 0)
                    $('#status').combobox('setValue',data.status);
                

                equipment.id = data.id;
                equipment.equpiment_id = data.equpiment_id;
                equipment.equipment_type = data.equipment_type;
                equipment.place_area = data.place_area;
                equipment.place_position = data.place_position;
                equipment.start_using_date = data.start_using_date;
                equipment.control_system_model = data.control_system_model;
                equipment.status = data.status;

                equipmentType.id = data.equipment_type;

                initDg(data.equpiment_id);
            }
        });
        



    }
    function initDg(equipment_id) {
        $.ajax({
            url:'/develop/url/getUrl.do',
            type: "POST",
            dataType: 'json',
            data: {
                'name': 'findEquipmentMaintainProjectDateByEquipmentId',
                'equipment_id':equipment_id
            },
            error: function() //失败
            {
            },
            success: function(data) //成功
            {
                var d = new Array();
                for (var i = 0; i < data.length; i++) {


                    var o = new Object();
                    var equipmentMaintainProjectDate = new Object();
                    equipmentMaintainProjectDate.id = data[i].id;
                    equipmentMaintainProjectDate.equipment_id = data[i].equipment_id;
                    equipmentMaintainProjectDate.maintain_project_name = data[i].maintain_project_name;
                    equipmentMaintainProjectDate.maintain_part = data[i].maintain_part;
                    equipmentMaintainProjectDate.maintain_project_id = data[i].maintain_project_id;
                    equipmentMaintainProjectDate.maintain_last_date = data[i].maintain_last_date;
                    equipmentMaintainProjectDate.maintain_next_date = data[i].maintain_next_date;
                    equipmentMaintainProjectDate.maintain_period_month = data[i].maintain_cycle;
                    equipmentMaintainProjectDate.maintain_notify_lead_time = data[i].ahead_notice_time;
                    equipmentMaintainProjectDate.maintain_cycle = data[i].maintain_cycle;
                    d.push(equipmentMaintainProjectDate);

                }
                $('#dg').datagrid({
                    data: d
                })
            }
        });
    }
    
    function loadAllImage(){
    	$.ajax({
            url: '/mro/equipment/findImgByEqpId',
            type: "post",
            dataType: 'json',
            data: {
                'equipment_id': equipment_id
            },
            error: function() //失败
            {

            },
            success: function(data) //成功
            {
            	console.log(JSON.stringify(data));
            	var selectImg = '';
            	var maxImg = '';
            	for (var i = 0; i < data.length; i++) {
            		selectImg+="<section class=\"image-section\">"+
                    "<div class=\"image-shade\"></div>"+
                    "<div class=\"image-zoom\"></div>"+
                    "<img class=\"image-show\" src='"+data[i].filePath+"' />"+
                    "<input id=\"src\" name=\"src\" value=\"\" type=\"hidden\" />"+
                "</section>";
            		maxImg+="<div id=\"zoom-shade\" style=\"display: none;\"></div>"+
            		"<div id=\"zoom-box\" style=\"display: none;\">"+
            			"<div id=\"zoom-content\"><img src='"+data[i].filePath+"'>"+
            			"</div>"+
            		"</div>"
            	}
            	$(".image-box").prepend(selectImg);
            	$(".upload-box").append(maxImg);
            	
            	
                
            }
        });
    }


</script>
<script type="text/javascript">
    var equipmentType = new Object();

    function loadMaintainProject(equipment_type) {
        //获取当前设备类型所含有的维护项目
        $.ajax({
            url: '/develop/url/getUrl.do',
            type: "GET",
            dataType: 'json',
            data: {
                'name': 'queryEquipmentMaintainProject',
                'equipment_type_id':equipment_type
            },
            error: function() //失败
            {

            },
            success: function(data) //成功
            {
                var d = new Array();
                for(var i=0;i<data.length;i++)
                {

                    var o = new Object();
                    o.id = data[i].id;
                    o.equipment_type_id = data[i].equipment_type_id;
                    o.maintain_notify_lead_time = data[i].maintain_notify_lead_time;
                    o.maintain_part = data[i].maintain_part;
                    o.maintain_period_month = data[i].maintain_period_month;
                    o.maintain_project_name = data[i].maintain_project_name;
                    o.maintain_project_id = data[i].id;
                    d.push(o);
                }
                $('#dg').datagrid({
                    data:d
                })
            }
        });
    }

</script>

<script type="text/javascript">
    $.extend($.fn.datagrid.methods, {
        editCell: function(jq, param) {
            return jq.each(function() {
                var opts = $(this).datagrid('options');
                var fields = $(this).datagrid('getColumnFields', true).concat($(this).datagrid('getColumnFields'));
                for (var i = 0; i < fields.length; i++) {
                    var col = $(this).datagrid('getColumnOption', fields[i]);
                    col.editor1 = col.editor;
                    if (fields[i] != param.field) {
                        col.editor = null;
                    }
                }
                $(this).datagrid('beginEdit', param.index);
                for (var i = 0; i < fields.length; i++) {
                    var col = $(this).datagrid('getColumnOption', fields[i]);
                    col.editor = col.editor1;
                }
            });
        }
    });

    var editIndex = undefined;

    function endEditing() {
        if (editIndex == undefined) {
            return true
        }
        if ($('#dg').datagrid('validateRow', editIndex)) {
            $('#dg').datagrid('endEdit', editIndex);
            editIndex = undefined;
            return true;
        } else {
            return false;
        }
    }

    function onClickCell(index, field) {
        if (endEditing()) {
            $('#dg').datagrid('selectRow', index)
                .datagrid('editCell', {
                    index: index,
                    field: field
                });
            editIndex = index;
        }
    }
</script>

<script type="text/javascript">

  function update() {

        if(!checkInput()) return;

        console.info(equipmentType);

        
        equipment.equipment_id = $('#equipment_id').textbox('getValue');
        equipment.equipment_type = equipmentType.id;
        equipment.place_area = $('#place_area').combobox('getValue');
        equipment.place_position = $('#place_position').textbox('getValue');
        equipment.start_using_date = $('#start_using_date').datebox('getValue');
        equipment.control_system_model = $('#control_system_model').textbox('getValue');
        equipment.status = $('#status').combobox('getValue');

        var list = new Array();
        var dg_list = $('#dg').datagrid('getRows');
        console.info(JSON.stringify(dg_list));
        for(var i=0;i<dg_list.length;i++)
        {
            var equipmentMaintainProjectDate = new Object();
            equipmentMaintainProjectDate.id = dg_list[i].id;
            equipmentMaintainProjectDate.equipment_id = $('#equpiment_id').textbox('getValue');
            equipmentMaintainProjectDate.maintain_project_id = dg_list[i].maintain_project_id;
            equipmentMaintainProjectDate.maintain_last_date = dg_list[i].maintain_last_date;
            equipmentMaintainProjectDate.maintain_next_date = dg_list[i].maintain_next_date;
            equipmentMaintainProjectDate.ahead_notice_time = dg_list[i].maintain_notify_lead_time;
            equipmentMaintainProjectDate.maintain_cycle = dg_list[i].maintain_period_month;
            list.push(equipmentMaintainProjectDate);
        }

        shuoheUtil.layerTopMaskOn();

        $.ajax({
            url: "/mro/equipment/equipment/update.do",
            type: "POST",
            dataType: 'json',
            data: {
                'equipment':JSON.stringify(equipment),
                'equipmentMaintainProjectDates':JSON.stringify(list)
            },
            error: function() //失败
            {
                shuoheUtil.layerTopMaskOff();
            },
            success: function(data) //成功
            {
                shuoheUtil.layerTopMaskOff();
                console.log(data);
                if (data.result == true) {
                    shuoheUtil.layerClose();
                    parent.updateSuccess();
                } else
                    shuoheUtil.layerMsgCustom(data.describe);
            }
        });
    }
    function onEnterDown() {
        if (window.event.keyCode == 13) {
            endEditing();
        }
    }
    function checkInput() {
        if (shuoheUtil.checkNull($('#equpiment_id').textbox('getValue'))) {
            shuoheUtil.layerMsgCustom('必须填写设备编号');
            return false;
        }
        // if (shuoheUtil.checkNull($('#equpiment_name').combogrid('grid').datagrid('getSelected'))) {
        //     shuoheUtil.layerMsgCustom('必须选择设备类型');
        //     return false;
        // }

        return true;
    }
</script>